#include "PlayState.h"

PlayState::PlayState(GameContext& gc)
	: GameState(gc)
{
}

PlayState::~PlayState()
{
}

void PlayState::Load()
{
	//mMap.Load("level01.txt", "texturepack01.txt");
	mCharacter.Load();

	char levelName[256];
	char texturePackName[256];
	sprintf_s(levelName, 256, "level0%d.txt", mGameContext.GetLevel());
	sprintf_s(texturePackName, 256, "texturepack0%d.txt", mGameContext.GetLevel());
	mMap.Load(levelName, texturePackName);
}

void PlayState::Unload()
{
	mMap.Unload();
	mCharacter.Unload();
}

NextState PlayState::Update(float deltaTime)
{
	mCharacter.Update(deltaTime, mMap);
	
	NextState nextState = None;
	if(Input_IsKeyPressed(Keys::ESCAPE))
		nextState = Frontend;

	return nextState;
}

void PlayState::Render()
{
	/////////////////////REFACTOR///////////////////////////
	//obtain screen center
	const int kWinWidth = IniFile_GetInt("WinWidth", 800);
	const int kWinHeight = IniFile_GetInt("WinHeight", 600);

	//target to be viewed in center of screen space
	//view target's position determines offset to keep world and screen space aligned
	SVector2 viewTarget = mCharacter.GetPosition();
	SVector2 viewOffset;

	//view target subtract half of screen
	//we use negative view target to move the world up and left
	viewOffset.x = viewTarget.x - (kWinWidth * 0.5f);
	viewOffset.y = viewTarget.y - (kWinHeight * 0.5f);

	//add a viewOffset limit so that we don't offset beyond the edge of the map
	SVector2 limit;
	limit.x = (float)mMap.GetWidth() - kWinWidth;
	limit.y = (float)mMap.GetHeight() - kWinHeight;

	//clamp grabs a floating point value,
	//if < minimum becomes minimum, if > maximum, becomes maximum
	viewOffset.x = Clamp(viewOffset.x, 0.0f, limit.x);
	viewOffset.y = Clamp(viewOffset.y, 0.0f, limit.y);
	/////////////////////////////////////////////////////////

	mMap.Render(viewOffset);
	mCharacter.Render(viewOffset);
}